const app = getApp();
import {
  post
} from '../../utils/request';
import {
  getPlaceInfo,
} from '../../utils/util';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    banner: null,

    analysis: {
      ingCount: 0,
      expiredCount: 0,
      userCount: 0,
    },

    listResult: {
      list: [],
      total: 0
    },
    listPayload: {
      pageNum: 1,
      pageSize: 10,
      param: {},
      loading: false,
      hasMore: true,
    }
  },

  // 统计数据
  getAnalysisData() {
    app.checkLogin(true, (loginInfo) => {
      this.setData({
        loginInfo: loginInfo,
      });
      post(`/campain/all/analysis`)
        .then(resp => {
          if (resp.code === 0) {
            this.setData({
              analysis: resp.data
            });
          } else {
            wx.showModal({
              title: '提示',
              content: resp.msg,
              showCancel: false,
            })
          }
        });
    });
  },

  toInfoPage(e) {
    let {
      info
    } = e.currentTarget.dataset;
    app.checkLogin(true, () => {
      wx.navigateTo({
        url: `/pages/campain/info?cid=${info.id}&type=${info.aboutType}&aid=${info.aboutId}`,
      });
    });
  },

  toMyList(e) {
    let {
      expired
    } = e.currentTarget.dataset;
    app.checkLogin(true, () => {
      wx.navigateTo({
        url: '/pages/campain/my?expired=' + expired,
      })
    });
  },

  // 获取列表
  getList() {
    let that = this;
    if (that.data.listPayload.loading || (!that.data.listPayload.hasMore)) {
      return;
    }
    app.checkLogin(true, () => {
      that.setData({
        listPayload: {
          ...that.data.listPayload,
          loading: true
        }
      });
      wx.showNavigationBarLoading();
      post("/campain/list", that.data.listPayload)
        .then(resp => {
          wx.stopPullDownRefresh()
          wx.hideNavigationBarLoading();
          let hasMore = true;
          if (0 == resp.code) {
            let listResult = that.data.listResult;
            let listPayload = that.data.listPayload;
            let list = (listPayload.pageNum === 1) ? resp.data.list : [...listResult.list, ...resp.data.list];
            list = list.map(d => {
              d.timeStart = d.timeStart.substring(0, 10);
              d.timeEnd = d.timeEnd.substring(0, 10);
              return d;
            });
            if (resp.data.list.length < listPayload.pageSize) {
              hasMore = false;
            }
            that.setData({
              listPayload: {
                ...listPayload,
                loading: false,
                hasMore
              },
              listResult: {
                total: resp.data.total,
                list
              }
            });
          } else {
            that.setData({
              listPayload: {
                ...listPayload,
                loading: false
              }
            });
          }
        })
    });
  },

  // 刷新列表
  refreshList() {
    this.setData({
      listResult: {
        list: [],
        total: 0
      },
      listPayload: {
        pageNum: 1,
        pageSize: 10,
        param: {},
        loading: false,
        hasMore: true,
      }
    });
    this.getList();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.refreshList();

    wx.setBackgroundColor({
      backgroundColor: "#ffffff"
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getAnalysisData();

    // 推广图片
    post(`/common/place/${app.globalData.tenantCode}/MP_SVIP_CAMPAIN`)
      .then(resp => {
        if (resp.code === 0) {
          this.setData({
            banner: getPlaceInfo(resp.data, 'MP_SVIP_CAMPAIN'),
          });
        }
      });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.refreshList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.listPayload.loading || (!this.data.listPayload.hasMore)) {
      return;
    }
    this.setData({
      listPayload: {
        ...this.data.listPayload,
        pageNum: this.data.listPayload.pageNum + 1
      }
    });
    this.getList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }

});